package com.qq.view;

import com.formdev.flatlaf.FlatLightLaf;
import com.qq.service.QQClientService;
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class QQViewModernGUI extends JFrame {
    private final CardLayout cardLayout = new CardLayout();
    private final JPanel mainPanel = new JPanel(cardLayout);

    private final JPanel loginPanel = new JPanel(new MigLayout("wrap, center", "[300]", "20[]20[]20[]20[]"));
    private final JTextField txtUserId = new JTextField(15);
    private final JPasswordField txtPassword = new JPasswordField(15);

    private final JPanel menuPanel = new JPanel(new BorderLayout(10,10));
    private final JLabel lblWelcome = new JLabel("", SwingConstants.CENTER);
    private final QQClientService clientService = new QQClientService();

    public QQViewModernGUI() {
        try { UIManager.setLookAndFeel(new FlatLightLaf()); }
        catch(Exception ex) { ex.printStackTrace(); }

        setTitle("网络通信系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(450, 380);
        setLocationRelativeTo(null);

        initLoginPanel();
        initMenuPanel();

        mainPanel.add(loginPanel, "login");
        mainPanel.add(menuPanel, "menu");
        add(mainPanel);
        setVisible(true);
    }

    private void initLoginPanel() {
        loginPanel.setBorder(new EmptyBorder(30, 30, 30, 30));
        JLabel lblTitle = new JLabel("欢迎登录", SwingConstants.CENTER);
        lblTitle.setFont(new Font("微软雅黑", Font.BOLD, 24));
        loginPanel.add(lblTitle, "span, align center, wrap 15");

        txtUserId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        txtPassword.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        loginPanel.add(new JLabel("账号:"), "align label");
        loginPanel.add(txtUserId, "growx");
        loginPanel.add(new JLabel("密码:"), "align label");
        loginPanel.add(txtPassword, "growx, wrap 20");

        JButton btnLogin = createButton("登录", new Color(0,153,204));
        loginPanel.add(btnLogin, "span, align center");

        btnLogin.addActionListener(e -> {
            String userId = txtUserId.getText().trim();
            String passwd = new String(txtPassword.getPassword());
            if(clientService.login(userId, passwd)) {
                lblWelcome.setText("欢迎用户：" + userId);
                cardLayout.show(mainPanel, "menu");
            } else {
                JOptionPane.showMessageDialog(this, "登录失败！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });
    }

    private void initMenuPanel() {
        menuPanel.setBorder(new EmptyBorder(20,20,20,20));
        lblWelcome.setFont(new Font("微软雅黑", Font.BOLD, 20));
        menuPanel.add(lblWelcome, BorderLayout.NORTH);

        JPanel funcPanel = new JPanel(new MigLayout("wrap 2", "[grow,fill][grow,fill]", "10[]"));
        JButton btnOnlineUsers = createButton("在线用户", new Color(76,175,80));
        JButton btnGroupMsg = createButton("群发消息", new Color(255,152,0));
        JButton btnPrivateMsg = createButton("私聊", new Color(33,150,243));
        JButton btnSendFile = createButton("发送文件", new Color(156,39,176));

        funcPanel.add(btnOnlineUsers);
        funcPanel.add(btnGroupMsg);
        funcPanel.add(btnPrivateMsg);
        funcPanel.add(btnSendFile);
        menuPanel.add(funcPanel, BorderLayout.CENTER);

        JButton btnExit = createButton("退出系统", new Color(220,53,69));
        menuPanel.add(btnExit, BorderLayout.SOUTH);

        btnExit.addActionListener(e -> {
            if(JOptionPane.showConfirmDialog(this, "确定退出吗？", "退出", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        });
    }

    private JButton createButton(String text, Color color) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.BOLD, 16));
        button.setBackground(color);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createEmptyBorder(12, 20, 12, 20));
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(QQViewModernGUI::new);
    }
}
